/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.locations.rooms;

import com.ewerp.mud.core.EwerpMud;
import com.ewerp.mud.core.exceptions.EwerpMudException;
import com.ewerp.mud.core.logging.ILog;
import com.ewerp.mud.core.plugin.EwerpPluginManager;
import com.ewerp.mud.core.plugin.IPlugin;
import com.ewerp.mud.entities.IObjectId;
import com.ewerp.mud.entitycore.descriptions.BaseDescription;
import com.ewerp.mud.entitycore.descriptions.meta.VisibleDescription;
import com.ewerp.mud.lifecycle.ILifecycleListener;
import com.ewerp.mud.locations.exits.IExit;
import com.ewerp.mud.storage.IPersistenceManager;

public class EwerpRoomManager implements IPlugin, IRoomManager, ILifecycleListener {
    protected static EwerpRoomManager instance = new EwerpRoomManager();

    protected IPersistenceManager persistenceManager;
    
    protected IRoom startRoom;
    
    protected EwerpRoomManager() {
    }
    
    public static EwerpRoomManager getInstance() {
        if(instance == null) {
            instance = new EwerpRoomManager();
        }
        return instance;
    }
    
    @Override
	public Class<?>[] getInterfaces() {
		return new Class<?>[] { IRoomManager.class };
	}
    
    @Override
    public IRoom getStartRoom() {
        return startRoom;
    }

    @Override
    public void init() {
    	try {
    		persistenceManager = (IPersistenceManager)EwerpPluginManager.getInstance().getPlugin(EwerpMud.defaultNamespace, IPersistenceManager.class);

    		persistenceManager.beginTransaction();
    		
    		startRoom = (IRoom)persistenceManager.fetchObject(BaseRoom.class);
    		
    		if(startRoom == null) {
    			// Build an initial starting room
    			startRoom = new BaseRoom();
    	        startRoom.setDescription(new BaseDescription(new VisibleDescription(), "Default starting room"));
    	                
    	        IRoomId roomId = new RoomId();
    	        roomId.setName("Default starting room");
    	        
    	        startRoom.setId(roomId);
    	        
	        	PropertyStartingRoom startRoomProperty = new PropertyStartingRoom();
	        	startRoom.getProperties().add(startRoomProperty);
	        	
	        	persistenceManager.saveObject(startRoom);
    		}
    		
    		persistenceManager.commitTransaction();
    	} catch (EwerpMudException e) {
        	System.err.println(e.getMessage());
        	e.printStackTrace();
        }
    }

    @Override
    public void pause() {
    }

    @Override
    public void start() {
    }

    @Override
    public void stop() {
    }

    @Override
    public IRoom getRoomById(IObjectId roomId) {
        IRoom result = null;
        
        return result;
    }
    
    @Override
    public void addRoom(IRoom room){
        
    }

    @Override
    public void addExit(IExit exit) {
        
    }

    @Override
    public IExit getExitById(IObjectId exitId) {
        IExit result = null;
        
        return result;
    }

}
